﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using VisualAgile.DTO;
using VisualAgile.Controllers;
using Spring.Context.Support;
using System.Web.Security;
using VisualAgile.Model;
using ProjectRouting.Controllers;

namespace VisualAgile.Web1
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               "ProjectIndex",
               "{projectName}",
               new { controller = "Project", action = "Index" }
            );

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "About", id = "" }  // Parameter defaults
            );

            //routes.MapRoute(
            //    "Default",                                              // Route name
            //    "{controller}.mvc/{action}/{id}",                           // URL with parameters
            //    new { controller = "Home", action = "About", id = "" }  // Parameter defaults
            //);

            //routes.MapRoute(
            //      "Root",
            //      "",
            //      new { controller = "Home", action = "About", id = "" }
            //    );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);           

            ModelBinders.Binders.Add(typeof(ReleaseDTO), new JsonBinder<ReleaseDTO>());
            ModelBinders.Binders.Add(typeof(FeatureDTO), new JsonBinder<FeatureDTO>());

            ModelBinders.Binders.Add(typeof(IterationDTO), new JsonBinder<IterationDTO>());
            ModelBinders.Binders[typeof(LabelDTO)] = new JsonBinder<LabelDTO>();
            ModelBinders.Binders.Add(typeof(StoryDTO), new JsonBinder<StoryDTO>());

            //ModelBinders.Binders.Add(typeof(TaskDTO), new JsonBinder<TaskDTO>());
            ModelBinders.Binders.Add(typeof(StatusDTO), new JsonBinder<StatusDTO>());

            ModelBinders.Binders.Add(typeof(UserDTO), new JsonBinder<UserDTO>());
            ModelBinders.Binders.Add(typeof(FeelingsDTO), new JsonBinder<FeelingsDTO>());

            ModelBinders.Binders[typeof(TaskDTO)] = new JsonBinder<TaskDTO>();
            ModelBinders.Binders[typeof(ActualsDTO)] = new JsonBinder<ActualsDTO>();
        }

        /// <summary>
        /// We want to replace the IPriciple generated by the ASP.NET runtime with our
        /// own User instance. This allows us to control access by role anwhere in 
        /// our application.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_OnAuthenticateRequest(Object sender, EventArgs e)
        {
            if (Context.User != null)
            {
                if (Context.User.Identity.IsAuthenticated)
                {

                    string name = Context.User.Identity.Name;

                    var webApplicationContext =
                                            ContextRegistry.GetContext();

                    AllManagerFactory manager = webApplicationContext.GetObject("ManagerFactory") as AllManagerFactory;

                    User user = null;
                    try
                    {
                        user = manager.UserManager.GetUser(new Guid(name));
                    }
                    catch
                    {
                        //this block is added for allowing to run demo instance without connecting to Database
                        //added for temprary purpose and will be removed.
                        user = DemoData.ManagerFactory.UserManager.GetByName("Manager");
                    }
                    

                    if (user == null)
                    {
                        FormsAuthentication.SignOut();
                    }
                    else
                    {
                        System.Threading.Thread.CurrentPrincipal = Context.User = user;
                        return;
                    }
                }
            }                       

            //System.Threading.Thread.CurrentPrincipal =
            //    Context.User = VisualAgile.Model.User.Guest;
        }
    }
}